package cn.dengta.webapp.im.dto;

import java.util.*;

import cn.dengta.common.model.ValueProxy;
import cn.dengta.common.util.Formats;
import cn.dengta.context.model.LabelI18nProxy;
import cn.dengta.context.model.UserEnumI18nKey;
import cn.dengta.webapp.base.entity.TimeBasedEntity;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.EasyDate;
import me.codeplayer.util.X;

/**
 * 电视墙配置表
 *
 * @author lgy
 * @since 2022-02-28
 */
@Getter
@Setter
public class TvWallConfig extends TimeBasedEntity {

	/** 钻石下限 */
	Integer coin;
	/** 停留时间（秒） */
	Integer stayTime;
	/** 类型：1=普通礼物；2=锁屏礼物 */
	Integer type;
	/** 排序 */
	Integer sort;

	public TvWallConfig(Integer type, Integer coin, Integer stayTime) {
		this.type = type;
		this.coin = coin;
		this.stayTime = stayTime;
		this.sort = 0;
	}

	public TvWallConfig(TvWallType type, Integer coin, Integer stayTime) {
		this(type.value, coin, stayTime);
	}

	public static final List<TvWallConfig> cache;

	static {
		final Date date = new EasyDate(2021, 12, 31).toDate();
		cache = Arrays.asList(
				new TvWallConfig(TvWallType.STD, 50, 4),
				new TvWallConfig(TvWallType.LOCK_SCREEN, 500, 10),
				new TvWallConfig(TvWallType.LOCK_SCREEN, 1000, 20),
				new TvWallConfig(TvWallType.LOCK_SCREEN, 5000, 30),
				new TvWallConfig(TvWallType.LOCK_SCREEN, 10000, 60)
		);
		for (TvWallConfig config : cache) {
			config.initTime(date);
		}
	}

	public boolean std() {
		return TvWallType.STD.eq(type);
	}

	public boolean lock() {
		return TvWallType.LOCK_SCREEN.eq(type);
	}

	public String getAddTime_() {
		return Formats.formatDate_DT(addTime);
	}

	public String getType_() {
		return X.map(TvWallType.of(type), TvWallType::getLabel);
	}

	@Getter
	public static enum TvWallType implements LabelI18nProxy<TvWallType, Integer> {

		/** 普通礼物 */
		STD(1, UserEnumI18nKey.TV_WALL_TYPE_STD),
		/** 锁屏礼物 */
		LOCK_SCREEN(2, UserEnumI18nKey.TV_WALL_TYPE_LOCK_SCREEN),
		//
		;

		// 定义私有变量
		public final Integer value;
		final ValueProxy<TvWallType, Integer> proxy;

		// 构造函数，枚举类型只能为私有
		TvWallType(Integer value, String label) {
			this.value = value;
			this.proxy = new ValueProxy<>(this, value, label);
		}

		public static TvWallType[] getValues() {
			return ValueProxy.getCachedArray(TvWallType.class, TvWallType::values);
		}

		public static TvWallType of(Integer value) {
			return STD.getValueOf(value);
		}
	}

	public static int calcIndex(final Integer coin, final List<TvWallConfig> cfgs) {
		int index = -1;
		if (coin == null || coin < 0 || !X.isValid(cfgs)) {
			return index;
		}
		for (int i = 0; i < cfgs.size(); i++) {
			if (coin < cfgs.get(i).getCoin()) {
				break;
			}
			index = i;
		}
		return index;
	}

}
